#include <iostream>
#include <thread>
#include <future>
#include <unistd.h>

int Add(int n1,int n2)
{
    std::cout << "------进行异步操作------" << std::endl;
    return n1 + n2;
}


int main(){
    //实现将加法函数使用future进行异步操作
    //defferred策略
    //在future对象get获取时才进行异步操作
    std::future<int> fu = std::async(std::launch::async,Add,11,12);
    // std::future<int> fu = std::async(std::launch::deferred,Add,11,12)
    std::this_thread::sleep_for(std::chrono::seconds(1));//休眠1s
    std::cout << "------1------" << std::endl;
    //当使用deferred策略时，异步操作会在get处执行
    int sum = fu.get();
    std::cout << sum << std::endl;
    return 0;
}